﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
using JobbJakt;

namespace JobbJaktWeb
{
    public partial class AvailableJobs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var user = Session["Users"] as List<Users>;
                ListView1.DataSourceID = advertisement.ID;
                ListView1.DataBind();
            }
        }

        protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        public void MsgBox(String ex, Page pg, Object obj)
        {
            string s = "<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
            Type cstype = obj.GetType();
            ClientScriptManager cs = pg.ClientScript;
            cs.RegisterClientScriptBlock(cstype, s, s.ToString());
        }

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            var item = e.Item as ListViewDataItem;
            int applicationID = int.Parse(ListView1.DataKeys[item.DisplayIndex].Value.ToString());

            var applicationObject = new Applications();
            
            try
            {
                applicationObject.AdvertisementID = applicationID;
                applicationObject.UserName = Session["UserName"].ToString();
                int affectedRows = Applications.InsertApplications(applicationObject);
                if (affectedRows == 0)
                {
                    MsgBox("Du har redan sökt detta jobb!", this.Page, this);
                }
                else
                {
                    MsgBox("Tack för din ansökan!", this.Page, this);
                }
                
            }
            catch (Exception ex)
            {
                Session["errormessage"] = ex.Message;
                Response.Redirect("Errorpage.aspx");
            }
        }
    }
}